package com.hy.mydesktop.client.mvc.core.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.component.type.ComponentControllerEnum;
import com.hy.mydesktop.client.component.type.ComponentTypeEnum;
import com.hy.mydesktop.shared.business.ToolBarHandler;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtAppEventType;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentControllerMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.MetaDataConstants;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceParameter;

public class ToolBarBuilderTemplate {

	public static GxtComponentControllerMetaNodeModel createToolBarBuilderTemplate(
			Map<String, Object> map1, Map<String, Object> map2) {
		
		Map<String , Object> toolBarController=new HashMap<String, Object>();
		toolBarController.put("component_controller_model_type", ComponentControllerEnum.TOOLBAR_CONTROLLER_MODEL);
		toolBarController.put("component_controller_model_type_index", ComponentControllerEnum.TOOLBAR_CONTROLLER_MODEL.ordinal());
		//List<String> codes4 = new ArrayList<String>();
	    //codes4.add("0301");
		//codes4.add("0401");
		//codes4.add("0007");
		//codes4.add("0008");
		//toolBarController.put("registereventtypes", codes4);
		//toolBarController.put("id", "3_1_toolBarController_PositonType");
		
		GxtComponentControllerMetaNodeModel node4=new GxtComponentControllerMetaNodeModel(toolBarController);
		node4 = ControllerAndComponentMetaNodeModelCreationTemplate.buildCommonAttributes(node4,map1);
		if (map1.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID)!=null) {	
			String controllerMetaModelId=(String)map1.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID);
			String id="toolBarController_"+controllerMetaModelId;
			node4.setId(id);
			node4.setControllerMetaNodeModelId(id);
		}else {
			throw new RuntimeException("xxxxxxxxxxxxx");
		}
		
		String[] appEventIds={"0301","0401"};
		node4.setRegisterEventTypes(node4.addGxtAppEventType(appEventIds));
		
		map2.put(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID, node4.getControllerMetaNodeModelId());
		GxtComponentMetaNodeModel gxtComponentMetaNodeModel =getToolBarGxtComponentMetaNodeModel(map2);
		node4.setGxtComponentMetaNodeModel(gxtComponentMetaNodeModel);


		return node4;
	}

	
	private static GxtComponentMetaNodeModel getToolBarGxtComponentMetaNodeModel(Map<String, Object> map2) {
		
		
	 	Map<String, Object> button01 = new HashMap<String, Object>();
	    button01.put("component_model_type_index", ComponentTypeEnum.BUTTON_MODEL.ordinal());   
	    button01.put("text", "新建");
		  //TODO map2.put("appEventType", AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
			//Map<String,Object> codes2 = new HashMap<String, Object>();
			//添加需要监听的事件类型，指定由某一个控制器响应，控制器内部应该如何响应（附加参数和数据）
			/**
			 * 00_01_02为特征码
			 * 或者是 控制器的唯一标识
			 */
			GxtAppEventType registerEventType = new GxtAppEventType();
			registerEventType.setRegisterEventType("0010");
					EventStructureMetaModel eventStructureMetaModel = new EventStructureMetaModel();
					eventStructureMetaModel.setControllerId("windowController2");
					eventStructureMetaModel.setRootControllerId("windowController2");
				
	
		//button01.put("registereventtypes", codes2);
		GxtToSeamServiceParameter gxtToSeamServiceParameter = new GxtToSeamServiceParameter();
			gxtToSeamServiceParameter.setServiceComponentName( "com.test.server.business.PersonService");
			//gxtToSeamServiceParameter.set(MetaDataConstants.SERVICE_COMPONENT_NAME, "PersonService");
			//gxtToSeamServiceParameter.set(MetaDataConstants.SERVICE_MEHOD_NAME, "loadSubApplication");
			gxtToSeamServiceParameter.setServiceMethodName( "loadSubApplication");
			//BaseModel[] methodArguments2 = new BaseModel[1];
			//methodArguments2[0] = new BaseModel();
		//	gxtToSeamServiceParameter.set("methodArguments",methodArguments2 );
		button01.put("gxttoseamserviceparametermodel", gxtToSeamServiceParameter);
		ItemOfAssociateToWindow node11  = new ItemOfAssociateToWindow(button01);
		node11.setEventStructureMetaModel(eventStructureMetaModel);
		node11.setRegisterEventType(registerEventType);
		node11.setGxtToSeamServiceParameter(gxtToSeamServiceParameter);
		
		Map<String, Object> button02 = new HashMap<String, Object>();
	    button02.put("component_model_type_index", ComponentTypeEnum.BUTTON_MODEL.ordinal());   
	    button02.put("text", "保存");
		ItemOfAssociateToWindow node22  = new ItemOfAssociateToWindow(button02);
		
		Map<String, Object> button03 = new HashMap<String, Object>();
	    button03.put("component_model_type_index", ComponentTypeEnum.BUTTON_MODEL.ordinal());   
	    button03.put("text", "清空");
		ItemOfAssociateToWindow node33  = new ItemOfAssociateToWindow(button03);
		
		Map<String, Object> button04 = new HashMap<String, Object>();
	    button04.put("component_model_type_index", ComponentTypeEnum.BUTTON_MODEL.ordinal());   
	    button04.put("text", "导入");
		ItemOfAssociateToWindow node44  = new ItemOfAssociateToWindow(button04);
		
		Map<String, Object> button05 = new HashMap<String, Object>();
	    button05.put("component_model_type_index", ComponentTypeEnum.BUTTON_MODEL.ordinal());   
	    button05.put("text", "导出");
	    ItemOfAssociateToWindow node55  = new ItemOfAssociateToWindow(button05);
	    
	   

		
	    List<ItemOfAssociateToWindow> set = new ArrayList<ItemOfAssociateToWindow>();
		set.add(node11);
		set.add(node22);
		set.add(node33);
		set.add(node44);
		set.add(node55);
		
		Map<String, Object> toolbar=new HashMap<String, Object>();
		toolbar.put("component_model_type_index", ComponentTypeEnum.TOOLBAR_MODEL.ordinal());
		toolbar.put("component_model_type", ComponentTypeEnum.TOOLBAR_MODEL);
//		toolbar.put("id", "03_01_toolbarGxtComponent_PositonType");	
		//toolbar.put("add", btn);
//		toolbar.put("children", list);
		
		
		toolbar.put(ComponentMetaDataConstants.CHILDREN_ITEMS, set);
		ToolBarHandler.configureToolBarGxtComponentMetaNodeModel ( toolbar );
		
		for (String key : map2.keySet()) {
			toolbar.put(key, map2.get(key));
		}
		GxtComponentMetaNodeModel gxtComponentMetaNodeModel=new GxtComponentMetaNodeModel(toolbar);
		//gxtComponentMetaNodeModel.setTitle(map2.get(ComponentMetaDataConstants.TITLE).toString());
		//gxtComponentMetaNodeModel.set(ComponentMetaDataConstants.HEADING, 
		//		map2.get(ComponentMetaDataConstants.HEADING).toString());
		if (map2.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID)!=null) {	
			String controllerMetaModelId=(String)map2.get(MetaDataConstants.COMPONENT_CONTROLLER_METAMODEL_ID);
			String id="toolBarGxtComponent_"+controllerMetaModelId;
			gxtComponentMetaNodeModel.setId(id);
		}else {
			throw new RuntimeException("xxxxxxxxxxxxx");
		}
		gxtComponentMetaNodeModel =ControllerAndComponentMetaNodeModelCreationTemplate.removeUnusedProperties(gxtComponentMetaNodeModel);

		return gxtComponentMetaNodeModel;
}


}
